
/**
 * 数据库操作
 * @auth ldy
 */
import { usqlite } from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
const Environment = plus.android.importClass('android.os.Environment');                   
usqlite.connect({
	name: 'ctappDB',// 数据库名称
	path: `${Environment.getExternalStorageDirectory()}/ctapp.db`, // 路径
}, (res, e) => {
	console.log(Environment.getExternalStorageDirectory())
	// 存储在/sdcard/ctapp.db 文件里
	console.log('链接数据库', res, JSON.stringify(e))
});
/**
 * 初始化
 */
export const init = () => {
	//如果table表格存在则连接，不存在则自动创建
	let dbModel = usqlite.model('tbuploadrecord',{
		id: {
			type: String,
			primaryKey: true
		},
	    devicesUseStatus: String,
	    recordType: String,
	    reportContent: String,
	    traceCode: String,
		devicesGeolocation: String,
		devicesGeolocationName: String,
		devicesClass: String,
		devicesName: String,
		devicesStatus: String,
		devicesModel: String,
		devicesManufacturer: String,
		devicesId: String,
		createDate: String
	});
	return dbModel
}
/**
 * 插入
 */
export const insertDB = (data) => {
	return new Promise((resolve) => {
		let dbModel = init()
		dbModel.insert(data, (err, results) => {
		    console.log(err);
		    console.log(results);
		    if (!err) console.log('增加成功');
			resolve(!err)
		});
	})
}

/**
 * 查询
 */
export const findDB = (traceCode) => {
	return new Promise((resolve) => {
		let dbModel = init()
		if (traceCode) {
			dbModel.find(`traceCode='${traceCode}'`, (err,results)=>{
			    console.log(results);
				resolve(results)
			});
		} else {
			dbModel.find((err,results)=>{
			    console.log(results);
				resolve(results)
			});
		}
		
	})
}

/**
 * 删除
 */
export const deleteDB = (id) => {
	return new Promise((resolve) => {
		let dbModel = init()
		dbModel.delete(`id='${id}'`,(err,results)=>{
		    console.log(err, results);
			resolve(!err)
		});
	})
}